package com.lu.pojo.reflection;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * @author luzhenfang
 * @date 2020 05 28
 * @description 反射泛型信息
 */
public class TestGeneric {
    public void test01(Map<String, Integer> map, List<Cat> list){
        System.out.println("test01");
    }
    public Map<String,Integer>test02(){
        return null;
    }
    public static void main(String[] args) throws NoSuchMethodException {
        // 通过反射获取 参数类型
        Method method = TestGeneric.class.getMethod("test01",Map.class,List.class);
        System.out.println(method);
        for (Type genericExceptionType : method.getGenericParameterTypes()) {
            System.out.println("#"+genericExceptionType);
            if(genericExceptionType instanceof ParameterizedType){
                for (Type actualTypeArgument : ((ParameterizedType) genericExceptionType).getActualTypeArguments()) {
                    System.out.println(actualTypeArgument);
                }
            }
        }
        // 反射获取泛型返回值
        Method test02 = TestGeneric.class.getMethod("test02");
        if (test02.getGenericReturnType() instanceof ParameterizedType){
            for (Type actualTypeArgument : ((ParameterizedType) test02.getGenericReturnType()).getActualTypeArguments()) {
                System.out.println("@"+actualTypeArgument.getTypeName());
            }
        }
    }
}
